// components/tabList.js
const network = require("../utils/network");
const util = require("../utils/util");
Component({
    options: {
        multipleSlots: true // 在组件定义时的选项中启用多 slot 支持
    },
    /**
     * 组件的属性列表
     */
    properties: {
        tabs: {
            type: Array,
            value: []
        },
        autoHideLoading: {
            type: Boolean,
            value: true
        },
        swipeable: {
            type: Boolean,
            value: true
        },
    },


    /**
     * 组件的初始数据
     */
    data: {
        useColor: util.themeColor,
        tabList: [],
        activeTabName: null,
        activeTabIdx: 0
    },


    /**
     * 组件的方法列表
     */
    methods: {
        onTabChange({
                        detail: {
                            index,
                            name,
                            title
                        }
                    }) {
            const _this = this
            _this.setData({
                activeTabName: name,
                activeTabIdx: index
            }, function () {
                _this.getList();
                _this.triggerEvent('changeTab', {id: name});
            })
        },

        getList(isAdd = false) {
            const _this = this
            const {
                tabKey,
                tab
            } = _this.getTabKey();
            return new Promise(async (resolve, reject) => {
                util.showLoading('加载中')
                console.log(tab.request.params)
                // if (!isAdd && !!reset) _this.resetTabList(false)
                network.GET({
                    uri: tab.request.url,
                    params: Object.assign({}, tab.listParams, tab.request.params, tab.request.searchParams || {}),
                    success(res) {
                        //console.log(res)
                        _this.setData({
                            [tabKey + '.listParams.page']: res.data.current,
                            [tabKey + '.listParams.total']: res.data.total,
                            [tabKey + '.listParams.pages']: res.data.pages || 1,
                            [tabKey + '.list']: isAdd ? ([].concat(tab.list, res.data.records || [])) : (res.data.records || [])
                        })
                        if (!isAdd) wx.stopPullDownRefresh()
                        _this.triggerEvent('updateList', {
                            index: _this.data.activeTabIdx,
                            list: _this.data.tabList[_this.data.activeTabIdx]
                        }, function () {
                            if (_this.properties.autoHideLoading) util.hideLoading();
                            resolve(res);
                        });
                    },
                    fail() {
                        if (!isAdd) wx.stopPullDownRefresh()
                        if (_this.properties.autoHideLoading) util.hideLoading()
                        reject()
                    }
                })
            })
        },
        getTabKey() {
            const _this = this
            const tabIndex = _this.data.activeTabIdx
            if (tabIndex === null || tabIndex === void 0) return Promise.reject(new Error('无标签数据'));
            //if (_this.data.tabList.length != _this.data.activeTabIdx + 1) return Promise.reject(new Error('标签数据异常'));
            return {
                tabKey: 'tabList[' + tabIndex + ']',
                tab: Object.assign({}, _this.data.tabList[tabIndex], _this.properties.tabs[tabIndex])
            }
        },
        handlePullDownRefresh() {
            const _this = this
            const {
                tabKey,
                tab
            } = _this.getTabKey();
            // let params = {};
            // if (!!_params) {
            //   const keys = Object.keys(_params) || [];
            //   keys.forEach(key => {
            //     if (_params[key] != null && _params[key] != undefined) params[key] = _params[key];
            //   });
            // }
            const listParams = Object.assign(tab.listParams, {
                page: 1
            });
            _this.setData({
                [tabKey + '.listParams']: listParams
            }, function () {
                _this.getList()
            })
        },
        handleReachBottom() {
            const _this = this
            const {
                tabKey,
                tab
            } = _this.getTabKey();
            console.log(tab);
            if (tab.listParams.pages > tab.listParams.page) {
                _this.setData({
                    [tabKey + '.listParams.page']: tab.listParams.page + 1
                }, function () {
                    _this.getList(true).catch(() => {
                        _this.setData({
                            [tabKey + '.listParams.page']: tab.listParams.page - 1
                        })
                    })
                })
            }
        },
        resetTabList(setCurTab = true) {
            const _this = this;
            return new Promise((resolve, reject) => {
                const list = (_this.properties.tabs || []);
                _this.setData({
                    tabList: list.map(d => {
                        return Object.assign({}, {
                            listParams: {
                                page_size: 10,
                                page: 1,
                                total: 0,
                                pages: 0
                            },
                            list: [],
                        }, d);
                    }),
                    activeTabName: setCurTab ? (list.length ? list[0].id : null) : _this.data.activeTabName,
                    activeTabIdx: setCurTab ? (list.length ? 0 : null) : _this.data.activeTabIdx
                }, function () {
                    resolve();
                })
            })
        },
        refreshCurTabList() {
            const _this = this;
            _this.resetTabList(false).then(() => _this.getList(false))
        }
    },

    // lifetimes: {
    //     attached() {
    //         const _this = this;
    //         _this.resetTabList(true).then(() => _this.getList())
    //     }
    // },

    pageLifetimes: {
        // 组件所在页面的生命周期函数
        show: function () {
            const _this = this;
            _this.resetTabList(true).then(() => _this.getList(false, false))
        },
    },
})
